package com.example.hrdemo.module.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hrdemo.module.entity.User;
import com.example.hrdemo.module.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper,User>{
    @Resource
    private UserMapper userMapper;

    //测试mybasit plus使用
//    public List<User> ceshiAll(){
//        return userMapper();
//    }

    //获取表内总行数
    public Long getUserCount(){
        return userMapper.selectCount(null);
    }

    //获取一个员工的信息
    public User getUserInfo(BigInteger id){
        return userMapper.selectById(id);
    }


    //获取所有员工的信息--分页
    public List<User> getAllPage(String keyword,Integer page,Integer pageSize){
        //分页参数
        Page<User> userPage = new Page<>(page, pageSize);
        //构建查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getIsDeleted,0);
        // 动态条件：keyword 模糊查询
//        这种写法是 MyBatis-Plus 中构建 嵌套条件组 的常见方式，可以总结为：
//        wrapper.条件方法(w -> w
//                .条件1().逻辑连接符().条件2()
//        );
        if (keyword != null && keyword != "") {
            wrapper.and(w -> w
                    .like(User::getUserName, keyword)   // 用户名模糊查询
            );
        }
        //.getRecords(); // 直接返回 List<User>
        return userMapper.selectPage(userPage,wrapper).getRecords();
    }

//    public List<User> getAllPage(String keyword,Integer page,Integer pageSize){
//        //偏移量的计算
//        Integer offset = (page-1)*pageSize;
//        return null;
////        return userMapper.getAllPage(keyword,pageSize,offset);
//    }

    //新增一名员工
    public int creatUser(String userName,Integer userGender,String userCardNum,String birthday, String education, String birthplace,String phone,String userWorkNum,
                         String jobName,Integer wages, String workDay, String formalWorkDay,Integer workAge,Integer userStatus,String picturesUrl){
        //时间戳
        int timestamp = (int) (System.currentTimeMillis() / 1000);

        User user = new User();
        user.setUserName(userName);
        user.setUserGender(userGender);
        user.setUserCardNum(userCardNum);
        user.setBirthday(birthday);
        user.setEducation(education);
        user.setBirthplace(birthplace);
        user.setPhone(phone);
        user.setUserWorkNum(userWorkNum);
        user.setJobName(jobName);
        user.setWages(wages);
        user.setWorkDay(workDay);
        user.setFormalWorkDay(formalWorkDay);
        user.setWorkAge(workAge);
        user.setUserStatus(userStatus);
        user.setPicturesUrl(picturesUrl);
        user.setCreateTime(timestamp);
        user.setUpdateTime(timestamp);
        user.setIsDeleted(0);
        return userMapper.insert(user);
    }

    //修改员工信息
    public int updateUser(BigInteger userId, String userName,Integer userGender,String userCardNum,String birthday, String education, String birthplace,String phone,
                          String userWorkNum,String jobName,Integer wages, String workDay, String formalWorkDay,Integer workAge,Integer userStatus,String picturesUrl){
        //时间戳
        int timestamp = (int) (System.currentTimeMillis() / 1000);

        User user = new User();
        user.setId(userId);
        user.setUserName(userName);
        user.setUserGender(userGender);
        user.setUserCardNum(userCardNum);
        user.setBirthday(birthday);
        user.setEducation(education);
        user.setBirthplace(birthplace);
        user.setPhone(phone);
        user.setUserWorkNum(userWorkNum);
        user.setJobName(jobName);
        user.setWages(wages);
        user.setWorkDay(workDay);
        user.setFormalWorkDay(formalWorkDay);
        user.setWorkAge(workAge);
        user.setUserStatus(userStatus);
        user.setPicturesUrl(picturesUrl);
        user.setUpdateTime(timestamp);

        return userMapper.updateById(user);
    }

    //删除一名员工（逻辑删除）
    public int deleteUser(BigInteger id){
        User user = new User();
        user.setId(id);
        //时间戳
        int timestamp = (int) (System.currentTimeMillis() / 1000);
        user.setIsDeleted(1);
        user.setUpdateTime(timestamp);
        return userMapper.updateById(user);
    }

}
